<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class HomeController extends Controller
{
    protected $sum_money;       //总资金
    protected $start_pt;        //开盘点位
    protected $total_num;       //总建仓数量
    protected $first_num;       //首次建仓数量
    protected $next_num;        //下次建仓数量
    protected $first_pt;        //首次建仓点位
    protected $next_pt;         //下次建仓点位
    protected $average_pt;      //平均建仓点位
    protected $cover_num;       //补仓次数
    protected $lever_multiple;  //杠杆倍数 用5/10/20 倍杠杆
    protected $lever_ratio;     //杠杆平仓比例
    protected $direction;       //方向 1 买入 2 卖出

    public function __construct()
    {
        //$this->sum_money        = 2000;
        $this->first_num        = 50400;
        $this->total_num        = $this->first_num;
        $this->next_num         = 28800;
        //$this->start_pt         = 11927;
        $this->first_pt         = 12215;
        $this->cover_num        = 5;
        $this->lever_multiple   = 10;
    }

    //TODO 每次建仓 数量 均保持一致 卖出方向为例  阶梯式数量为最佳比例
    public function index()
    {
        $data = $this->computeAveragePt($this->first_pt,$this->cover_num);
        $data['first_pt'] = $this->first_pt;
        dump($data);
        $money = 2000;
        $arr = [];
        for ($i = 0; $i < 200; $i++){
            $money *= 1.03;
            $arr[] = $money;
        }
        dump($arr);
    }

    //计算不同杠杆强平仓比例
    protected function computeLeverRatio()
    {
        return 1.191;//5倍
        //return 1.091;//10倍
        //return 1.042;//20倍
    }

    /**
     * 计算下一个补仓点位
     * @param   int  $prev_pt       上一个建仓点位，默认为首次建仓点位
     * @param   int  $cover_num     建仓次数
     *
     * @return  array
     *
     */
    protected function computeAveragePt($prev_pt, $cover_num, &$pt_arr = [])
    {
        $next_pt = intval($this->computeLeverRatio() * $prev_pt);
        //总建仓数量
        $this->total_num += $this->next_num;
        //计算下次建仓数量占比
        $next_ratio = floor(($this->next_num / $this->total_num) * 100) / 100;
        //计算两次建仓点位差值
        $pt_diff = $next_pt - $prev_pt;
        //平均建仓点位 等于上一个建仓的点位
        $average_pt = $prev_pt + intval($next_ratio * $pt_diff);
        $pt_arr['next_pt'][]    = $next_pt;
        $pt_arr['average_pt'][] = $average_pt;
        $pt_arr['loss'][]       = ($next_pt/$average_pt -1) * 100 * $this->lever_multiple;
        $pt_arr['amplitude'][]  = ($next_pt/$this->first_pt -1) * 100;
        //递归
        if ($cover_num > 1){
            $this->computeAveragePt($average_pt, $cover_num -1, $pt_arr);
        }
        return $pt_arr;
    }

    //下次建仓点位占比
    protected function nextPtRatio()
    {
        //$next_ratio = floor(($this->next_num / $this->total_num) * 100) / 100;
    }



}
